//
//  YZXCalendarCollectionViewCell.m
//  YZXCalendar
//
//  Created by 尹星 on 2017/6/28.
//  Copyright © 2017年 尹星. All rights reserved.
//

#import "YZXCalendarCollectionViewCell.h"
#import "YZXCalendarModel.h"
#import "YZXCalendarHelper.h"
#import <Masonry/Masonry.h>

@interface YZXCalendarCollectionViewCell ()

//@property (weak, nonatomic) IBOutlet UILabel *day;
//@property (weak, nonatomic) IBOutlet UIView *bgView;
//@property (weak, nonatomic) IBOutlet UIView *instructionsView;
//@property (weak, nonatomic) IBOutlet UIView *bottomInstructionsView;

@property (strong, nonatomic)  UILabel *day;
@property (strong, nonatomic)  UIView *bgView;
@property (strong, nonatomic)  UIView *instructionsView;
@property (strong, nonatomic)  UIView *bottomInstructionsView;
@end

@implementation YZXCalendarCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self steupUi];
    }
    return self;
}

- (void)steupUi {
    UIView *bottomInstructionsView = [[UIView alloc] init];
    bottomInstructionsView.backgroundColor = [UIColor colorWithRed:171/255.0 green:185/255.0 blue:196/255.0 alpha:1.0];
    _bottomInstructionsView = bottomInstructionsView;
    [self.contentView addSubview:bottomInstructionsView];
    [bottomInstructionsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.centerY.equalTo(self.contentView);
        make.height.mas_equalTo(40);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    _bgView = bgView;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    UIView *instructionsView = [[UIView alloc] init];
    instructionsView.backgroundColor = [UIColor colorWithRed:171/255.0 green:185/255.0 blue:196/255.0 alpha:1.0];
    _instructionsView = instructionsView;
    [bgView addSubview:instructionsView];
    [instructionsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.centerY.equalTo(bgView);
        make.height.equalTo(bottomInstructionsView.mas_height);
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:14.0];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = @"";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.numberOfLines = 1;
    _day = titleLabel;
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(bgView);
    }];
    [self layoutIfNeeded];
  
}

- (void)layoutContentViewOfCollectionViewCellWithCellIndxePath:(NSIndexPath *)indexPath model:(YZXCalendarModel *)model
{
    self.bgView.backgroundColor = [UIColor whiteColor];
    YZXCalendarDayModel *dayModel = model.dayArray[indexPath.row];
    self.day.text = dayModel.day;
    self.userInteractionEnabled = YES;
    self.day.textColor = [UIColor blackColor];
    [self.bgView.layer setCornerRadius:0];
    [self.bgView.layer setMasksToBounds:YES];
    self.instructionsView.hidden = YES;
    self.bottomInstructionsView.hidden = YES;
    if (!self.day.text.length) {
        self.userInteractionEnabled = NO;
    }
    if (dayModel.isOverTheWeekend) {
      self.day.textColor = [UIColor redColor];
    }
    //今天
    if ([YZXCalendarHelper.helper determineWhetherForTodayWithIndexPaht:indexPath model:model] == YZXDateEqualToToday) {
        self.day.text = @"Today";
        self.day.textColor = [UIColor redColor];
    } else if ([YZXCalendarHelper.helper determineWhetherForDay:YZXCalendarHelper.helper.maxEndDate withIndexPaht:indexPath model:model] == YZXDateLaterThanToday) {//判断日期是否超过max
        self.day.textColor = [UIColor grayColor];
        self.userInteractionEnabled = NO;
    } else if ([YZXCalendarHelper.helper determineWhetherForDay:YZXCalendarHelper.helper.minStartDate withIndexPaht:indexPath model:model] == YZXDateEarlierThanToday) {
        self.day.textColor = [UIColor grayColor];
        self.userInteractionEnabled = NO;
    }
}

- (void)changeContentViewBackgroundColor:(UIColor *)backgroundColor
{
    self.bgView.backgroundColor = backgroundColor;
}


- (void)changeDayTextColor:(UIColor *)textColor
{
    self.day.textColor = textColor;
}

- (void)showInstructionsView {
    self.instructionsView.hidden = NO;
}


- (void)showBottomInstructionsViewMasksToLeft {
    self.bottomInstructionsView.hidden = NO;
    [self setCornerOnLeft:self.bottomInstructionsView.bounds.size.height *0.5 view:self.bottomInstructionsView];
}

- (void)showBottomInstructionsViewMasksToRight {
    self.bottomInstructionsView.hidden = NO;
    [self setCornerOnRight:self.bottomInstructionsView.bounds.size.height *0.5 view:self.bottomInstructionsView];
}
- (void)masksToBoundsCornerRadius
{
    [self.bgView.layer setCornerRadius:self.bgView.bounds.size.width * 0.5];
    [self.bgView.layer setMasksToBounds:YES];
}

- (NSString *)getTheCellDayText
{
    return self.day.text;
}

/*设置左边圆角*/
- (void)setCornerOnLeft:(CGFloat )cornerRadius view:(UIView *)view {
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
    byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft)
    cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
}

/*设置右边圆角*/
- (void)setCornerOnRight:(CGFloat )cornerRadius view:(UIView *)view {
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
    byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight)
    cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
}

@end
